package com.yc.baihe.biz.impl;

import java.util.ArrayList;
import java.util.List;

import com.yc.baihe.beans.AdminUser;
import com.yc.baihe.biz.IAdminUserBiz;
import com.yc.baihe.dao.DBHelper;
import com.yc.baihe.dao.MD5Encryption;

public class AdminUserBizImpl implements IAdminUserBiz {
	DBHelper db=new DBHelper();
	
	public int addAdminInfo(AdminUser au) {
		String sql="insert into adminUser values(seq_adminUser_aid.nextval,?,?,?,?,1,'','')";
		List<Object> params=new ArrayList<Object>();
		params.add(au.getPwds());
		params.add(au.getAname());
		params.add(au.getTel());
		params.add(au.getEmail());
		return db.update(sql, params);
	}

	public int delAdminInfo(String aid) {
		if(aid.indexOf("or")>0){
			return 0;
		}else{
			String sql="delete from adminUser where aid in("+aid+")";
			return db.update(sql, null);
		}
	}

	public AdminUser loginAdmin(String aid, String pwd) {
		String sql="select aid,pwd from adminUser where aid=? and pwd=?";
		List<Object> params=new ArrayList<Object>();
		params.add(aid);
		params.add(MD5Encryption.createPassword(pwd));
		List<AdminUser> list=db.find(sql,params,AdminUser.class);
		if(list!=null && list.size()>0 ){
			return list.get(0);
		}else{
			return null;
		}
	}

	public int updateAdmin(AdminUser au) {
		String sql="update adminUser set aid=aid";
		List<Object> params=new ArrayList<Object>();
		if(au.getAname()!=null){
			sql+=",aname=?";
			params.add(au.getAname());
		}
		if(au.getPwds()!=null){
			sql+=",pwd=?";
			params.add(au.getPwds());
		}
		if(au.getEmail()!=null){
			sql+=",email=?";
			params.add(au.getEmail());
		}
		if(au.getTel()!=null){
			sql+=",tel=?";
			params.add(au.getTel());
		}
		sql+=" where aid=?";
		params.add(au.getAid());
		return db.update(sql, params);
	}

	public List<AdminUser> find(Integer pageNo, Integer pageSize) {
		String sql="select * from(select a.*,rownum rn from (select aid,aname,pwd,tel,email,status from adminUser order by aid) a where rownum<=?) b where rn>?";
		List<Object> params=new ArrayList<Object>();
		params.add(pageNo*pageSize);
		params.add((pageNo-1)*pageSize);
		return db.find(sql,params,AdminUser.class);
	}

	public int getTotal() {
		String sql="select count(aid) from adminUser";
		List<List<String>> list=db.finds(sql, null);
		if(list!=null && list.size()>0){
			return Integer.parseInt(list.get(0).get(0));
		}
		return 0;
	}

}
